﻿@{
    ViewBag.Title = "商品折扣信息管理";
}
@section headerSection{
    <style>
        #exampleToolbar > .form-group {
            float: left;
            margin: 0;
            margin-left: 5px;
        }
    </style>
}
<div class="animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins" style="margin-bottom: 0px;">
        <div class="ibox-content" style="padding: 5px 20px 0px 20px;">
            <div class="row row-lg">
                <div class="col-sm-12 col-sm-leftRight">
                    <!-- Example Toolbar -->
                    <div class="example">
                        <div class="btn-group hidden-xs" id="exampleToolbar" role="group">
                            <button id="disDetail" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-th-large" aria-hidden="true">详细</i>
                            </button>
                            <button id="disAdd" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-plus" aria-hidden="true">添加</i>
                            </button>
                            <button id="disEdit" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-pencil" aria-hidden="true">编辑</i>
                            </button>
                            <button id="disDel" type="button" class="btn btn-outline btn-danger">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true">删除</i>
                            </button>
                            <div class="form-group">
                                <button id="btnSearch" class="btn btn-primary" type="button"><i class="fa fa-search"></i>&nbsp;刷新</button>
                            </div>
                        </div>
                        <table id="exampleTableToolbar" data-mobile-responsive="true" data-radio="true"></table>
                    </div>
                    <!-- End Example Toolbar -->
                </div>
            </div>
        </div>
    </div>
    <!-- End Panel Other -->
</div>
<!--  添加商品折扣 -->
<div id="sellForm" style="width: 580px; overflow: hidden; display: none;">
    <form class="form-horizontal m-t" id="sellFormTable" method="post">
        <div class="form-group">
            <label class="col-sm-3 control-label">商品名称<span class="glyphicon glyphicon-asterisk requiredtitle"></span>：</label>
            <div class="col-sm-8">
                <input id="fileName" name="fileName" type="text" disabled="disabled" class="form-control input-sm" />
                <input type="hidden" id="id" name="id" />
                <input type="hidden" id="file_name" name="file_name" />
                <input type="hidden" id="file_code" name="file_code" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">用户等级<span class="glyphicon glyphicon-asterisk requiredtitle"></span>：</label>
            <div class="col-sm-8">
                <select id="user_level" name="user_level" title="请选择用户等级" class="form-control input-sm selectpicker" data-live-search="true" required="" aria-required="true" placeholder="请选择用户等级" style="padding: 5px 10px;"></select>
            </div>
        </div>
        <div class="form-group" id="markPass">
            <label class="col-sm-3 control-label">商品折扣<span class="glyphicon glyphicon-asterisk requiredtitle"></span>：</label>
            <div class="col-sm-8">
                <input id="rate" name="rate" type="text" autoComplete="off" required="" aria-required="true" class="form-control input-sm" placeholder="例如：8折请输入80" title="例如：8折请输入80" />
            </div>
        </div>      
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea class="form-control" id="note" name="note" rows="3" placeholder="请输入备注信息" title="请输入备注信息"></textarea>
            </div>
        </div>
    </form>
</div>
@section footSection{
    <script>
        var $table, options,fileName,fileCode;
        var levelManager = {
            initPage: function () {
                if (!levelManager.initData())
                    return false;
                levelManager.initSelect();
                levelManager.initTable();
                levelManager.bindEvent();
            },
            initData:function(){
                fileName = $.getUrls("fileName");
                fileCode = $.getUrls("fileCode");
                if (!fileName || !fileCode) {
                    layer.alert("商品信息不允许为空！");
                    return false;
                }
                fileName = decodeURI(fileName);
                return true;
            },
            initSelect:function(){
                $('#user_level').initSelectpicker("UserLevel");
            },
            initTable: function () {
                options = {
                    url: ctx + "/AccountManager/GetGoodDisData",
                    cutHeight: 5,
                    toolbar: "#exampleToolbar",
                    queryParams: function (params) {
                        params = {
                            //页面大小
                            rows: params.limit,
                            //第几页
                            page: params.offset / params.limit + 1,
                            fileCode: fileCode
                        }
                        return params;
                    },
                    columns: [
                         {
                             radio: true
                         }, {
                             field: '',//第一列序号
                             title: '序号',
                             align: 'center',
                             width: 50,
                             formatter: function (value, row, index) {
                                 return index + 1;
                             }
                         }, {
                             field: 'user_level',
                             title: '用户等级',
                             align: 'center'
                         }, {
                             field: 'rate',
                             title: '折扣',
                             align: 'center'
                         }, {
                             field: 'note',
                             title: '备注',
                             align: 'left'
                         }, {
                             field: 'update_date',
                             title: '最后更新时间',
                             align: 'center',
                             formatter: function (value, row, index) {
                                 if (value)
                                     return $.formatByDate(value);
                                 return "";
                             }
                         }, {
                             field: 'update_name',
                             title: '最后更新人',
                            align: 'center'
                        }, {
                            field: 'create_date',
                            title: '创建时间',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (value)
                                    return $.formatByDate(value);
                                return "";
                            }
                        }, {
                            field: 'create_name',
                            title: '创建人',
                            align: 'center'
                        }]
                }
                $table = tableHelper.initTable("exampleTableToolbar", options);
            },
            //获取选中行数据
            selections: function (isAll) {
                var row = $table.bootstrapTable('getSelections');
                if (!row || row.length === 0) {
                    layer.msg("数据为空或未选中记录行~", { icon: 5 });
                    return false;
                }
                if (isAll)
                    return row;
                if (row.length > 1) {
                    layer.msg("当前操作不支持多选，请选择一行再操作！", { icon: 5 });
                    return false;
                }
                return row[0];
            },
            bindEvent: function () {
                $('#btnSearch').on('click', function () {
                    $table.bootstrapTable('refresh', options);
                });
                $('#disDetail').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    //清空表单
                    $('#sellFormTable').resetHideValidForm();
                    layer.open({
                        title: ['商品折扣详细信息', 'font-size:18px;'],
                        type: 1,
                        content: $('#sellForm'),
                        scroll: true,//是否显示滚动条、默认不显示
                        btn: ['确定', '取消'],
                        area: ['600px', '500px'],
                        success: function (li, o) {
                            if (row != null) {
                                $('#sellFormTable')[0].reset();//重置表单
                                $('#sellFormTable').LoadForm(row);//表单填充数据
                            }
                        }
                    });
                });
                $('#disAdd').on('click', function () {
                    levelManager.editForm();
                });
                $('#disEdit').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    levelManager.editForm(row);
                });
                $('#disDel').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    layer.confirm('您确定要删除当前选择商品折扣记录？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                        $.ajax({
                            type: 'post',
                            url: ctx + '/AccountManager/DelGoodDisById/' + row.id,
                            dataType: 'json',
                            async: false,
                            success: function (data) {
                                layer.close(index);
                                if (data.Statu === 0) {
                                    layer.msg("删除成功...");
                                    $table.bootstrapTable('refresh');
                                }
                                else
                                    layer.msg(data.Msg, { icon: 5 });
                            }
                        });
                    }, function () { });
                });
            },
            //编辑、添加
            editForm: function (row) {
                var $title = "添加商品折扣信息";
                if (!row)
                    addOrEditUrl = ctx + "/AccountManager/AddGoodDis";//新增
                else {
                    addOrEditUrl = ctx + "/AccountManager/EditGoodDis";//编辑
                    $title = "编辑商品折扣信息";
                }
                //清空表单
                $('#sellFormTable').resetHideValidForm();
                //打开编辑
                layer.open({
                    title: [$title, 'font-size:18px;'],
                    type: 1,
                    content: $('#sellForm'),
                    scroll: true,//是否显示滚动条、默认不显示
                    btn: ['确定', '取消'],
                    area: ['600px', '500px'],
                    success: function (li, o) {
                        if (row) {
                            $('#sellFormTable')[0].reset();//重置表单
                            $('#sellFormTable').LoadForm(row);//表单填充数据
                            $('#fileName').val(row.file_name);
                        } else {
                            $('#fileName').val(fileName);
                            $('#file_name').val(fileName);
                            $('#file_code').val(fileCode);
                        }
                    },
                    yes: function (li, o) {
                        if ($('#sellFormTable').valid()) {
                            //验证通过
                            $('#sellFormTable').ajaxSubmit({
                                url: addOrEditUrl,
                                type: "post",
                                dataType: "json",
                                beforeSubmit: function (arr, $form, options) {
                                    layer.msg("提交数据~", { icon: 1, time: 5000 });
                                },
                                success: function (result, status, xhr, $form) {
                                    if (result.Statu === 0) {
                                        layer.closeAll();
                                        $table.bootstrapTable('refresh');
                                        layer.msg('保存成功', { icon: 1 });
                                    }
                                    else
                                        layer.alert(result.Msg, { icon: 5 });
                                },
                                error: function (xhr, status, error, $form) {
                                    layer.msg("保存失败~", { icon: 5 });
                                }
                            });
                        }
                    },
                    cancel: function (li, o) {

                    }
                });
            }
        }
        $(function () {
            levelManager.initPage();
        });
    </script>
}
